package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletPaymentSettingContract;
import com.meitianhui.convenitenbuy.module.wallet.di.DaggerWalletComponent;
import com.meitianhui.convenitenbuy.module.wallet.di.WalletModule;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletPaymentSettingPresenter;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.PasswordDialog;

import javax.inject.Inject;

public class WalletPaymentSettingActivity extends BaseToolbarActivity implements WalletPaymentSettingContract.View, PasswordDialog.OnDialogPsdCompleteListener {
    EditText mEt;
    Button mBtn;
    private PasswordDialog passwordDialog;
    @Inject
    WalletPaymentSettingPresenter presenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerWalletComponent.builder().appComponent(App.getInstance().getAppComponent()).walletModule(new WalletModule(this)).build().inject(this);
        setTbTitle(getString(R.string.wallet_payment_setting));
        initId();
        isEnable(false);
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_payment_setting;
    }

    @Override
    protected void onRefreshData() {

    }

    private void initId() {
        mEt = getViewById(R.id.et_wallet_payment_set);
        mBtn = getViewById(R.id.btn_wallet_payment_set);
        RxUtils.bindEvents(mBtn, this);
        mEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        mEt.setText(s);
                        mEt.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    mEt.setText(s);
                    mEt.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        mEt.setText(s.subSequence(0, 1));
                        mEt.setSelection(1);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_wallet_payment_set:
                checkInput();
                break;
        }
    }

    private void checkInput() {
        presenter.checkInput(mEt.getText().toString().trim());
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void checkResult(int result) {
        switch (result) {
            case Constants.WALLET_PAYMENT_SETTING_ERROR:
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_payment_setting_error), 3);
                mEt.setText("");
                break;
            case Constants.WALLET_PAYMENT_SETTING_FAIL:
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_payment_setting_fail), 3);
                mEt.setText("");
            case Constants.WALLET_PAYMENT_SETTING_WRONG:
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_payment_setting_wrong), 3);
                mEt.setText("");
                break;
            case Constants.WALLET_PAYMENT_SETTING_EMPTY:
                TipUtils.showToast(App.getInstance(), getString(R.string.wallet_payment_setting_input), 3);
                mEt.setText("");
                break;
        }
    }

    @Override
    public void checkPassWord(String input) {
        passwordDialog = new PasswordDialog(this, R.layout.dialog_payment_pwd);
        passwordDialog.setOnDialogPsdCompareListener(this);
        passwordDialog.show();
        passwordDialog.setPsdDescription(getString(R.string.dialog_password_description));
        passwordDialog.setPsdContent(input);
    }

    @Override
    public void checkResultSuccess(String input) {
        if (passwordDialog != null)
            passwordDialog.dismiss();
        Intent intent = new Intent(this, WalletPaymentShowActivity.class);
        intent.putExtra(Constants.ARGUMENT, mEt.getText().toString().trim());
        startActivity(intent);
        finish();
    }


    @Override
    public void onPasswordCorrect(PasswordDialog dialog) {
        passwordDialog.dismiss();
        Intent intent = new Intent(this, WalletPaymentShowActivity.class);
        intent.putExtra(Constants.ARGUMENT, mEt.getText().toString().trim());
        startActivity(intent);
        finish();
    }

    @Override
    public void onPasswordError(PasswordDialog dialog, String msg) {
        TipUtils.showToast(App.getInstance(), msg);
        passwordDialog.clearInput();
    }
}
